#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using sqengine.Engine.Common;
#endregion

namespace sqengine.PhysicObjects
{

    public class ReflectObject : PhysicObject
    {
        Model _model;
        Effect _effecttype;
        Texture2D _environment;
        Camera _camera;
        Quaternion _rotation;
        Vector3 _position;

        public ReflectObject(Game game, Model model, float radius, Matrix orientation, Vector3 position, Effect Effectype, Texture2D Environment, Camera camera)
            : base(game, model)
        {
            _rotation = new Quaternion(0, 0, 0, 1);
            _position = position;
            _camera = camera;
            _effecttype = Effectype;
            _environment = Environment;
            _model = model;
            //model = null;

            body = new Body();
            collision = new CollisionSkin(body);
            collision.AddPrimitive(new Sphere(Vector3.Zero * 5.0f,radius), new MaterialProperties(0.5f,0.7f,0.6f));
            body.CollisionSkin = this.collision;
            Vector3 com = SetMass(10.0f);
            body.MoveTo(position + com, orientation);
            
            
            collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity));
            body.EnableBody();
            this.scale = Vector3.One * radius;
        }

        public override void ApplyEffects(BasicEffect effect)
        {
            //effect.DiffuseColor = color;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Matrix world = Matrix.CreateScale(Vector3.One) * Matrix.CreateFromQuaternion(_rotation) * Matrix.CreateTranslation(_position);
            Matrix wvp = world * _camera.View * _camera.Projection;
            

            if (_effecttype.Parameters["world"] != null)
                _effecttype.Parameters["world"].SetValue(world);
            if (_effecttype.Parameters["wvp"] != null)
                _effecttype.Parameters["wvp"].SetValue(wvp);

            if (_effecttype.Parameters["EyePosition"] != null)
                _effecttype.Parameters["EyePosition"].SetValue(_camera.Position);

            if (_effecttype.Parameters["cubeMap"] != null)
                _effecttype.Parameters["cubeMap"].SetValue(_environment);

            if (_effecttype.Parameters["Time"] != null)
                _effecttype.Parameters["Time"].SetValue((float)gameTime.TotalGameTime.TotalSeconds);

            if (_effecttype.Parameters["tint"] != null)
                _effecttype.Parameters["tint"].SetValue(Color.White.ToVector4());
            

            for (int pass = 0; pass < _effecttype.CurrentTechnique.Passes.Count; pass++)
            {
                for (int msh = 0; msh < model.Meshes.Count; msh++)
                {
                    ModelMesh mesh = model.Meshes[msh];
                    for (int prt = 0; prt < mesh.MeshParts.Count; prt++)
                        mesh.MeshParts[prt].Effect = _effecttype;
                    
                    mesh.Draw();
                }
            }




            //base.Draw(gameTime);
        }
    }
}
